//
//  MyTableViewVC.swift
//  studyRxSwiftDemo
//
//  Created by jemi on 2019/12/18.
//  Copyright © 2019 jemi. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import RxDataSources

class MyTableViewVC: UIViewController {
    
    lazy var tableView:UITableView = {
        let tableView = UITableView()
        tableView.tableFooterView = UIView()
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (m) in
            m.edges.equalTo(0)
        }
        return tableView
    }()
    
    let mylistViewModel = MyListViewModel()
    let disposebag = DisposeBag()
    var dataSource:RxTableViewSectionedReloadDataSource<MySections>!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "cell")
        dataSource = RxTableViewSectionedReloadDataSource<MySections>(configureCell: { (dataSource, tableView, indexPath, item) -> MyTableViewCell in
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MyTableViewCell
            cell.setupModel(item)
            
            return cell
            })
        tableView.rx.setDelegate(self).disposed(by: disposebag)
        let m = My(name: "jiuge", age: 15, sex: "男")
        let m1 = My(name: "lafite", age: 16, sex: "男")
        let m2 = My(name: "joke", age: 17, sex: "男")
        let m3 = My(name: "lisa", age: 18, sex: "女")
//        Observable.just("")
//        Observable.of("","").subscribe(<#T##observer: ObserverType##ObserverType#>)
//        Observable.from(T##array: [_]##[_])
  
        mylistViewModel.obs.subscribe(onNext: { (t) in
            
        }).disposed(by: disposebag)
        
        
        
        Observable.of([MySections(items: [m,m1,m2,m3])]).asDriver(onErrorJustReturn: []).drive(tableView.rx.items(dataSource: dataSource)).disposed(by: disposebag)
        
        tableView.rx.modelSelected(My.self).subscribe(onNext: { (my) in
            print(my.name)
        }).disposed(by: disposebag)
        
        tableView.rx.itemSelected.subscribe(onNext: { (indexPath) in
            print("选择了第 \(indexPath.row)条")
        }).disposed(by: disposebag)
        
    }
}

extension MyTableViewVC: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
}
